package com.example.orderfoodsystem22201310liuziming.controller;

import com.example.orderfoodsystem22201310liuziming.entity.allorder;
import com.example.orderfoodsystem22201310liuziming.entity.shoppingorder;
import com.example.orderfoodsystem22201310liuziming.entity.shoppingcart;
import com.example.orderfoodsystem22201310liuziming.repository.AllOrderRepository;
import com.example.orderfoodsystem22201310liuziming.repository.CartRepository;
import com.example.orderfoodsystem22201310liuziming.repository.OrderRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

@Controller
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private HttpSession httpSession;
    @Autowired
    private OrderRepository orderRepository;
    @Autowired //接线
    private CartRepository cartRepository;
    @Autowired
    private AllOrderRepository allOrderRepository;


    @RequestMapping("/addOrder2")                //购物车添加到订单
    protected String addOrder2(Model model){
        //------------------------添加-----------------------------------------------------------------------------------
        String username = (String)httpSession.getAttribute("aaaa");
        List<Integer> numlist = Arrays.asList(1,0);
        allorder allorder = allOrderRepository.findByTablenumberAndStatusIn((int)httpSession.getAttribute("tableID"),numlist);
        if(null == allorder) {  //检验餐桌是否有在用订单
            //本地此刻
            LocalDateTime ldt = LocalDateTime.now();
            //解析 格式化器
            DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH-mm-ss");

            for (Iterator<shoppingcart> it = cartRepository.findAllByUsername(username).iterator(); it.hasNext(); ) {
                shoppingcart x = it.next();
                shoppingorder shoppingorder = new shoppingorder();
                shoppingorder.setFoodID(x.getFoodID());
                shoppingorder.setFoodname(x.getFoodname());
                shoppingorder.setFoodprice(x.getFoodprice());
                shoppingorder.setFoodnumber(x.getFoodnumber());
                shoppingorder.setUsername(x.getUsername());
                shoppingorder.setOrdertime(dtf.format(ldt));
                shoppingorder.setTablenumber((int) httpSession.getAttribute("tableID"));
                orderRepository.save(shoppingorder);
            }
            //---------------------删除购物车原菜品----------------------------------------------------------------------------
            cartRepository.deleteAllByUsername(username);
            httpSession.setAttribute("finish", 1);
            //---------------------记录用户时间-------------------------------------------------------------------------------
            allorder AllOrder = new allorder();
            AllOrder.setUsername(username);
            AllOrder.setTime(dtf.format(ldt));
            AllOrder.setTablenumber((int) httpSession.getAttribute("tableID"));
            AllOrder.setStatus(1);
            allOrderRepository.save(AllOrder);
            return "redirect:/buyer/order";
        }else {
            model.addAttribute("msg","当前餐桌还在使用");
            return "/buyer/BuyerMessage";
        }

    }

    @RequestMapping("/finishOrder/{username}") //卖家出餐
    protected String finishOrder(Model model,@PathVariable("username")String username){
        allorder allorderOptional = allOrderRepository.findByUsername(username);
        if(allorderOptional.getStatus()==1) {
            allorderOptional.setStatus(0);  //餐桌状态更新
            allOrderRepository.save(allorderOptional);
        }
        return "redirect:/seller/order2";
    }















}
